import { money_trans } from '@/utils/utils';
import { Divider, Badge, Descriptions, Table, Tag } from 'antd';
import { useEffect, useState } from 'react';
import ProTable from '@ant-design/pro-table';
const OrderCommon = ({ payOrderValues, refund }) => {
  const RefoundTags = (payload, type) => {
    let color;
    let val;
    if (type === 'refundStatus') {
      switch (payload) {
        case 0:
          color = 'blue';
          val = '未退款';
          break;
        case 1:
          color = 'cyan';
          val = '退款中';
          break;
        case 2:
          color = 'green';
          val = '退款成功';
          break;
        case 3:
          color = 'red';
          val = '退款失败';
          break;
        case 4:
          color = 'geekblue';
          val = '取消退款';
          break;
        default:
          break;
      }
    } else if (type === 'payType') {
      switch (payload) {
        case 1:
          color = 'green';
          val = '现金支付';
          break;
        case 2:
          color = 'green';
          val = '刷卡支付';
          break;
        case 3:
          color = 'green';
          val = '扫码支付';
          break;
        case 4:
          color = 'green';
          val = '余额支付';
          break;
        case 5:
          color = 'green';
          val = '积分支付';
          break;
        case 9:
          color = 'green';
          val = '微信支付';
          break;
        default:
          break;
      }
    } else if (type === 'isMember') {
      switch (payload) {
        case true:
          color = 'green';
          val = '是';
          break;
        case false:
          color = '';
          val = '否';
          break;
        default:
          break;
      }
    }

    return (
      <Tag color={color} key={payload}>
        {val}
      </Tag>
    );
  };
  const activityColumns = [
    {
      title: '活动名称',
      dataIndex: 'activityName',
      valueType: 'text',
      render: (dom, entity) => {
        return <a>{dom}</a>;
      },
    },
    {
      title: '活动类型',
      dataIndex: 'subType',
      valueEnum: {
        1: {
          text: '满减',
          status: 'Processing',
        },
        2: {
          text: '满送',
          status: 'Success',
        },
        3: {
          text: '满送',
          status: 'Success',
        },
        4: {
          text: '满送积分',
          status: 'Success',
        },
      },
    },
    {
      title: '活动范围',
      dataIndex: 'allStore',
      hideInForm: true,
      hideInSearch: true,
      valueEnum: {
        true: {
          text: '全场通用',
          status: 'Processing',
        },
        false: {
          text: '部分店铺',
          status: 'Success',
        },
      },
    },
    {
      title: '满',
      dataIndex: 'activityFull',
      valueType: 'money',
    },

  ];
  const payColumns = [
    {
      title: '商品名称',
      dataIndex: 'goodsName',
      valueType: 'text',
      render: (dom, entity) => {
        return <a>{dom}</a>;
      },
    },
    {
      title: '商品规格',
      dataIndex: 'goodsSpec',
      valueType: 'text',
    },
    {
      title: '商品单位',
      dataIndex: 'goodsUnit',
      valueType: 'text',
    },
    {
      title: '单价',
      dataIndex: 'price',
      valueType: 'text',
      render: (dom, entity) => {
        return money_trans(dom,true);
      },
    },
    {
      title: '购买数量',
      dataIndex: 'count',
      valueType: 'text',
    },
    {
      title: '是否会员价',
      dataIndex: 'isMember',
      valueEnum: {
        true: {
          text: '是',
          status: 'Success',
        },
        false: {
          text: '否',
          status: 'Error',
        },
      },
      render: (text) => {
        return <>{RefoundTags(text, 'isMember')}</>;
      },
    },
    {
      title: '总金额',
      dataIndex: 'totalAmount',
      valueType: 'text',
      render: (dom, entity) => {
        return money_trans(dom,true);
      },
    },
    {
      title: '配送费',
      dataIndex: 'freightAmount',
      valueType: 'text',
      render: (dom, entity) => {
        return money_trans(dom,true);
      },
    },
    {
      title: '支付方式',
      dataIndex: 'payType',
      valueEnum: {
        1: {
          text: '现金支付',
          status: 'Processing',
        },
        2: {
          text: '刷卡支付',
          status: 'Success',
        },
        3: {
          text: '扫码支付',
          status: 'Error',
        },
        4: {
          text: '余额支付',
          status: 'Error',
        },
        5: {
          text: '积分支付',
          status: 'Error',
        },
        9: {
          text: '微信支付',
          status: 'Success',
        },
      },
      render: (text) => {
        return <>{RefoundTags(text, 'payType')}</>;
      },
    },
    {
      title: '退款状态',
      dataIndex: 'refundStatus',
      valueEnum: {
        0: {
          text: '未退款',
          status: 'Default',
        },
        1: {
          text: '退款中',
          status: 'Processing',
        },
        2: {
          text: '退款成功',
          status: 'Error',
        },
        3: {
          text: '退款失败',
          status: 'Error',
        },
        4: {
          text: '取消退款',
          status: 'Error',
        },
      },
      render: (text) => {
        return <>{RefoundTags(text, 'refundStatus')}</>;
      },
    },

    {
      title: '退款金额',
      dataIndex: 'refundAmount',
      render: (dom, entity) => {
        return money_trans(dom,true);
      },
    },
    {
      title: '退款数量',
      dataIndex: 'refundCount',
    },
    {
      title: '购买店铺',
      dataIndex: 'storeName',
      valueType: 'text',
    },
    {
      title: '下单前库存',
      dataIndex: 'beforeStock',
      valueType: 'text',
    },
    {
      title: '下单后库存',
      dataIndex: 'afterStock',
      valueType: 'text',
    },
    {
      title: '操作',
      dataIndex: 'option',
      render: (_, record) => {
        return <>{refund ? refund(record) : null}</>;
      },
    },
  ];

  useEffect(() => {
    return refund ? null : payColumns.splice(payColumns.length - 1, 1);
  }, []);
  return (
    <div style={{ width: '100%', height: '100%' }}>
      <Divider orientation="left">订单详情</Divider>
      <Descriptions column={4}>
        <Descriptions.Item label="会员名称">
          {payOrderValues ? payOrderValues.memberName : '暂无'}
        </Descriptions.Item>
        <Descriptions.Item label="会员卡号">
          {payOrderValues ? payOrderValues.memberNumber : '暂无'}
        </Descriptions.Item>
        <Descriptions.Item label="会员手机号">
          {payOrderValues ? payOrderValues.memberMobile : '暂无'}
        </Descriptions.Item>
        <Descriptions.Item label="支付通道名称">
          {payOrderValues ? payOrderValues.channelName : '暂无'}
        </Descriptions.Item>
        <Descriptions.Item label="支付通道编号">
          {payOrderValues ? payOrderValues.channelNumber : '暂无'}
        </Descriptions.Item>
        <Descriptions.Item label="上游订单号">
          {payOrderValues ? payOrderValues.upperNumber : '暂无'}
        </Descriptions.Item>
        <Descriptions.Item label="订单号">
          {payOrderValues ? payOrderValues.number : '暂无'}
        </Descriptions.Item>

        <Descriptions.Item label="支付状态">
          {(() => {
            switch (payOrderValues ? payOrderValues.status : null) {
              case 0:
                return <Badge status="default" text="未支付" />;
              case 1:
                return <Badge status="processing" text="支付中" />;
              case 2:
                return <Badge status="success" text="支付成功" />;
              case 3:
                return <Badge status="error" text="支付失败" />;
              default:
                break;
            }
          })()}
        </Descriptions.Item>
        <Descriptions.Item label="回调状态">
          {(() => {
            switch (payOrderValues ? payOrderValues.callbackStatus : null) {
              case 0:
                return <Badge status="default" text="未回调" />;
              case 1:
                return <Badge status="success" text="回调成功" />;
              case 2:
                return <Badge status="error" text="回调失败" />;
              default:
                break;
            }
          })()}
        </Descriptions.Item>
        <Descriptions.Item label="支付时间">
          {payOrderValues ? new Date(payOrderValues.payTime).toLocaleString() : '暂无'}
        </Descriptions.Item>
        <Descriptions.Item label="支付方式">
          <span style={{ fontWeight: 'bolder' }}>
            {' '}
            {(() => {
              if (payOrderValues) {
                switch (payOrderValues.payType) {
                  case 1:
                    return '现金支付';
                  case 2:
                    return '刷卡支付';
                  case 3:
                    return '扫码支付';
                  case 4:
                    return '余额支付';
                  case 5:
                    return '积分支付';
                  case 9:
                    return '微信支付';
                  default:
                    return '暂无';
                }
              }
            })()}
          </span>
        </Descriptions.Item>
        <Descriptions.Item label="积分比例">
          {payOrderValues ? payOrderValues.scale : '暂无'}
        </Descriptions.Item>
        <Descriptions.Item label="订单原总金额">
          {payOrderValues ? money_trans(payOrderValues.totalOriginAmount,true) : '暂无'}
        </Descriptions.Item>
        <Descriptions.Item label="订单总金额">
          {payOrderValues ? money_trans(payOrderValues.totalAmount,true) : '暂无'}
        </Descriptions.Item>

        <Descriptions.Item label="订单总配送费">
          {payOrderValues ? money_trans(payOrderValues.freightAmount,true) : '暂无'}
        </Descriptions.Item>
        <Descriptions.Item label="订单类型">
          {payOrderValues ? (payOrderValues.type === 1 ? '消费' : '会员消费') : '暂无'}
        </Descriptions.Item>
        <Descriptions.Item label="送货地址">
          {payOrderValues ? payOrderValues.address : '暂无'}
        </Descriptions.Item>
        <Descriptions.Item label="物流单号">
          {payOrderValues ? payOrderValues.logisticsNumber : '暂无'}
        </Descriptions.Item>
        <Descriptions.Item label="销售人员">
          {payOrderValues ? payOrderValues.sellerName : '暂无'}
        </Descriptions.Item>
        <Descriptions.Item label="订单备注">
          {payOrderValues ? (
            <div dangerouslySetInnerHTML={{ __html: payOrderValues.remark }}></div>
          ) : (
            '暂无'
          )}
        </Descriptions.Item>
        <Descriptions.Item label="订单来源">
          {payOrderValues?.isFromwx ? '科施福惠民购' : '暂无'}
        </Descriptions.Item>
      </Descriptions>
      {
        (payOrderValues.memberNumber!=='188' && (payOrderValues.beforeBalance>0 || payOrderValues.afterBalance>0 || payOrderValues.beforeIntegral>0 || payOrderValues.afterIntegral>0))?
          (
            <>
              <Divider orientation="left">账户信息</Divider>
              <Descriptions column={2}>
              <Descriptions.Item label="支付前余额">
                {payOrderValues ? money_trans(payOrderValues.beforeBalance,true) : '暂无'}
              </Descriptions.Item>
              <Descriptions.Item label="支付后余额">
                {payOrderValues ? money_trans(payOrderValues.afterBalance,true) : '暂无'}
              </Descriptions.Item>
              <Descriptions.Item label="支付前积分">
                {payOrderValues ? money_trans(payOrderValues.beforeIntegral,true) : '暂无'}
              </Descriptions.Item>
              <Descriptions.Item label="支付后积分">
                {payOrderValues ? money_trans(payOrderValues.afterIntegral,true) : '暂无'}
              </Descriptions.Item>
              </Descriptions>
            </>
          )
          :null
      }
      {
        payOrderValues?.activityInfo?
          (
            <>
              <Divider orientation="left">参加活动</Divider>
              <ProTable
                bordered
                columns={activityColumns}
                rowKey="id"
                search={false}
                options={false}
                toolbar={false}
                pagination={false}
                dataSource={ payOrderValues?.activityInfo || []}
              />
            </>
          )
          :null
      }
      <Divider orientation="left">订单明细</Divider>
      <Table
        bordered
        columns={payColumns}
        rowKey={r=>r.id}
        dataSource={ payOrderValues?.children || []}
      />
    </div>
  );
};

export default OrderCommon;
